<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <link rel="stylesheet" th:href="@{/js/layuiadmin-1.2.1/style/dtree/dtree.css}" media="all"/>
    <link rel="stylesheet" th:href="@{/js/layuiadmin-1.2.1/style/dtree/font/dtreefont.css}" media="all">
    <th:block th:include="include :: header('组织机构')" />
</head>
<body class="no-skin">
<style>
    .west {width:310px;border-top: 15px solid #f2f2f2;}
    .layui-body {left:310px; border: 16px solid #F2f2f2; border-bottom: 0;padding: 0 16px}
    .layui-side-scroll{border-right: 1px solid #f6f6f6;width:310px;}
    .layui-logo{color: #fff;height: 41px;line-height: 41px;font-size: 16px}
    .layui-logo span{display: block;width: 277px;float: left;margin-left: 10px;}
    .layui-nav {background: #FFF;color:#000;width:310px;}
    .layui-col-sm4 {width: 23%;}
    .layui-btn-xs {height: 32px;line-height: 32px;padding: 0 16px;font-size: 12px;}
    .layui-icon-shrink-right:before {background-color:rgba(243,160,65,0.9)}
    .layui-icon-spread-left:before {background-color:rgba(243,160,65,0.9)}
</style>
<div class="main-container" id="main-container">
    <div class="main-content">
        <div class="west layui-side">
            <div class="layui-side-scroll">
                <div class="layui-logo">
                    <span>组织机构</span>
                    <a href="javascript:;" class="menuBtn">
                        <i class="layui-icon layui-icon-shrink-right" id="LAY_app_flexible" style="color: #fff;float: right"></i>
                    </a>
                </div>
                <div class="layui-nav layui-nav-tree">
                    <div style="text-align: center;margin: 12px 0 5px;">
                        <button class="layui-btn layui-btn-xs layui-btn-primary" dtree-id="dhtree" dtree-menu="jigou" title="新增公司"><i class="dtreefont dtree-icon-fenzhijigou"></i></button>
                        <button class="layui-btn layui-btn-xs layui-btn-primary" dtree-id="dhtree" dtree-menu="bumen" title="新增部门"><i class="dtreefont dtree-icon-yonghu"></i></button>
                        <button class="layui-btn layui-btn-xs layui-btn-primary" dtree-id="dhtree" dtree-menu="bianji" title="编辑"><i class="dtreefont dtree-icon-bianji"></i></button>
                        <button class="layui-btn layui-btn-xs layui-btn-primary" dtree-id="dhtree" dtree-menu="shanchu" title="删除"><i class="dtreefont dtree-icon-roundclosefill"></i></button>
                    </div>
                    <div id="dhtree" class="demo-tree demo-tree-box"></div>
                </div>
            </div>
            <div class="panel layout-expand layui-expand-west" id="panel" style="display: none; width: 114px; left: 0px; top: 0px;height: 41px;color: #fff;">
                <div class="panel-header">
                    <div class="panel-tool"  id="panel-tool" style="text-align: center; width: 114px;height: 41px;line-height: 41px;font-size: 16px;">
                        <span class="panel-body panel-title" style="width: 114px;">组织机构</span>
                        <a href="javascript:;" class="menuBtn">
                            <i class="layui-icon layui-icon-spread-left" id="LAY_app_flexible" style="color: #fff;float: right"></i>
                        </a>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-body" id="LAY_app_body">
            <div class="north">
                <div class="north-content">
                    <form class="layui-form dis-ctr" >
                        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                            <div class="layui-form-item">
                                <div class="layui-inline layui-col-sm4">
                                    <label class="layui-form-label">名称：</label>
                                    <div class="layui-input-block">
                                        <input id="vname" type="text" name="vname" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-inline" style="line-height: 34px;">
                                    <a class="layui-btn layuiadmin-btn-order dhicon dh-search" id="btn-search"><span class="fa_span">查询</span></a>
                                    <button type="reset" class="layui-btn layui-btn-primary dhicon dh-refresh" id="dh-resets"><span class="fa_span">重置</span></button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
            <div class="mid-content">
                <table class="layui-hide" id="dhtable1" lay-filter="ftable1"></table>
                <table class="layui-hide" id="dhtable" lay-filter="ftable"></table>
            </div>
        </div>
    </div>
</div>
<div id="jigou" style="display: none;width: 80%;margin-top: 10px;">
    <form class="post_form">
        <input type="hidden" name="guid" id="guid">
        <input type="hidden" name="pguid" id="pguid">
        <input type="hidden" name="orgKind" id="orgKind">
        <div class="layui-form-item">
            <label class="layui-form-label">上级：</label>
            <div class="layui-input-block">
                <input type="text" id="pname" readonly="readonly" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><font class="f_red">*</font>名称：</label>
            <div class="layui-input-block">
                <input type="text" name="orgName" id="orgName" autocomplete="off" class="layui-input">
            </div>
        </div>
    </form>
</div>
<script type="text/html" id="managerBarDemo">
{{# if(d.managerOrg == '【上级】管理者'){ }}
	<a class="layui-btn layui-btn-disabled layui-btn-xs dhicon dh-remove" >删除</a>
{{# }else{}}
	<a class="layui-btn layui-btn-danger layui-btn-xs dhicon dh-remove" lay-event="del">删除</a>
{{# } }}
</script>
<script type="text/html" id="barDemo">
	<a class="layui-btn layui-btn-danger layui-btn-xs dhicon dh-remove" lay-event="del">删除</a>
</script>
<script type="text/html" id="toolbar1">
    <div class="layui-btn-container">
		<div class="layui-btn layui-btn-normal layui-table-tools" lay-event="addorgmanager">
			<i class="dhicon dh-plus"></i> 新增管理者
        </div>
    </div>
</script>
<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
		<div class="layui-btn layui-btn-normal layui-table-tools" lay-event="adduser">
			<i class="dhicon dh-plus"></i> 新增员工
        </div>
    </div>
</script>
<th:block th:include="include :: footer" />
<script th:src="@{/dhecp/js/left-tree.js?v=2}"></script>
<script type="text/javascript">
fromModule = "organization";
// $(".layui-logo").css("background",$(".layui-layout-admin .layui-logo", parent.document.body).css("backgroundColor"));
$(".layui-logo").css({"background":pellucidityTen,"text-align":"center"});
// $(".layui-expand-west").css("background",$(".layui-layout-admin .layui-logo", parent.document.body).css("backgroundColor"));
$(".layui-expand-west").css({"background": pellucidityTen,position: "absolute"});
    var vtype = "",pguid="",pOrgName="",ppOrgName="",orgPathAll='orgPathAll',childlist=[];
    var vname = "无",pvname = "无",guid = 'null',CommonTreet;
    layui.extend({
        dtree : '/js/layuiadmin-1.2.1/lib/extend/dtree-tp'
    }).use(['table','form','layer','jquery', 'util','dtree'], function () {
        var $ = layui.$,layer = layui.layer,table = layui.table,form = layui.form,util = layui.util,dtree = layui.dtree;
        var treeData = getTree('/system/organization/listtreejson');
        CommonTreet = dtree.render({
            elem: "#dhtree",
            menubar:true,
            record:true,
            data: treeData,
            menubarTips:{
                freedom:[{
                    menubarId:"jigou",handler:function(node){nodeHandler(node,'新增公司','addjigou','company');}
                },{
                    menubarId:"bumen",handler:function(node){nodeHandler(node,'新增部门','addbumen','dept');}
                },{
                    menubarId:"bianji",handler:function(node){nodeHandler(node,'修改','edit');}
                },{
                    menubarId:"shanchu",handler:function(node){nodeHandler(node,'删除','delete');}
                }
                ],
                group:[] // 按钮组制空
            }
        });
        //取消树节点选中
        $(document).bind('click', function(event) {
            var evt = event.srcElement ? event.srcElement : event.target;
            evt = $(evt);
            if(evt.hasClass("layui-nav-tree") || evt.parents(".layui-nav-tree").length != 0 || evt.hasClass("layui-layer-page") || evt.parents(".layui-layer-page").length != 0
                || evt.hasClass("layui-body") || evt.parents(".layui-body").length != 0 || evt.hasClass("layui-layer-btn") || evt.parents(".layui-layer-btn").length != 0
                || evt.hasClass("layui-layer-setwin") || evt.parents(".layui-layer-setwin").length != 0)
                return;
            else{
                CommonTreet.cancelNavThis();
                vtype = "",pguid="",guid="",pOrgName="",ppOrgName="";
            };
        });
        function nodeHandler(node,dname,action,orgKind){
            var purl;
            if(Object.keys(node).length==0 && action != 'addjigou'){
                layer.msg("请选择节点");
            }else{
                if(orgKind)$("#orgKind").val(orgKind);
                if(action == 'addjigou' && vtype =='dept'){
                    layer.msg("部门下面不能添加公司！");
                    return false;
                }
                if(action == 'addjigou' && vtype =='user'){
                    layer.msg("员工下面不能添加公司！");
                    return false;
                }
                if(action == 'addbumen' && vtype =='user'){
                    layer.msg("员工下面不能添加部门！");
                    return false;
                }
                if(action != 'edit' && action != 'delete'){
                    $('.post_form')[0].reset();
                    $("#pguid").val(pguid);
                    if(pOrgName){
                        $("#pname").val(pOrgName);
                    }else{
                        $("#pname").val("顶级");
                        $("#pguid").val("root");
                    }
                    purl = '/system/organization/addpost';
                }else{
                    ppOrgName?$("#pname").val(ppOrgName):$("#pname").val("顶级")
                    $("#guid").val(pguid);
                    $("#orgName").val(node.context);
                    $("#orgKind").val(vtype);
                    purl = '/system/organization/editorgnamepost';
                }
                if(action == 'delete'){
                	var parentObj = dtree.getParentParam(CommonTreet, pguid);
                	pguid = parentObj.nodeId;
                	pOrgName = parentObj.context;
                    if(Object.keys(childlist).length > 0){
                        $.modal.confirmCom('当前选中的节点下有子级，确定要一起删除么？',function(){deletepost(orgPathAll);$('.post_form')[0].reset();});
                    }else{
                        $.modal.confirmCom('你确定删除么？',function(){deletepost(orgPathAll);$('.post_form')[0].reset();});
                    }
                }else{
                    $.modal.openHtml(dname,'400','280',"jigou",purl,"post_form",function(result){
                        allReload(result);
                    },'',true);
                }
            }
        }
        dtree.on("node('dhtree')" ,function(obj){
            var nodeJson = JSON.parse(obj.param.recordData);    //后台传回的节点参数（字符串）
            vtype = nodeJson.vtype;pguid = obj.param.nodeId;pOrgName = obj.param.context;ppOrgName = obj.parentParam.context;childlist = obj.childParams;orgPathAll = nodeJson.orgPathAll;
            table.reload('dhtable1', {url: "/system/organization/managersAlllistjson?orgPathAll="+orgPathAll,page:{curr: 1}});
            table.reload('dhtable', {url: "/system/organization/userlistjson?orgPathAll="+orgPathAll,page:{curr: 1}});
        });
        //管理员列表
        $.tablelist.init({
            elem: '#dhtable1'
            ,url: "/system/organization/managersAlllistjson?orgPathAll="+orgPathAll
            ,cols: [[
                {type:'numbers', fixed: 'left'}
                ,{fixed:'left', title: '操作',align: 'center',width: 100, toolbar: '#managerBarDemo'}
                ,{field:'userId', title: '用户编号',align: 'center',width: 100}
                ,{field:'userName', title: '用户名称',align: 'center',width: 100}
                ,{field:'userLevel', title: '用户级别',align: 'center',width: 100}
                ,{field:'managerOrg', title: '管理者来源',align: 'center',width: 150}
                ,{field:'cid', title: '公司编号',align: 'center',width: 100}
                ,{field:'cidName', title: '公司名称',align: 'center',width: 180}
                ,{field:'deptId', title: '部门编码',align: 'center',width: 100}
                ,{field:'deptName', title: '部门名称',align: 'center',width: 100}
                ,{field:'deptPath', title: '部门全路径',align: 'center',width: 100}
                ,{field:'deptPathName', title: '部门全路径名称',align: 'center',width: 280}
                ,{field:'guid', title: 'ID',hide:true}
            ]
            ]
            ,toolbar: '#toolbar1'
            ,height:260
            ,width:'full-' + 260
        })
        //员工列表
        $.tablelist.init({
            elem: '#dhtable'
            ,url: "/system/organization/userlistjson?orgPathAll="+orgPathAll
            ,cols: [[
                {type:'numbers', fixed: 'left'}
                ,{fixed:'left', title: '操作',align: 'center',width: 100, toolbar: '#barDemo'}
                ,{field:'userId', title: '用户编号',align: 'center',width: 100}
                ,{field:'userName', title: '用户名称',align: 'center',width: 100}
                ,{field:'userLevel', title: '用户级别',align: 'center',width: 100}
                ,{field:'cid', title: '公司编号',align: 'center',width: 100}
                ,{field:'cidName', title: '公司名称',align: 'center',width: 180}
                ,{field:'deptId', title: '部门编码',align: 'center',width: 100}
                ,{field:'deptName', title: '部门名称',align: 'center',width: 100}
                ,{field:'deptPath', title: '部门全路径',align: 'center',width: 100}
                ,{field:'deptPathName', title: '部门全路径名称',align: 'center',width: 280}
                ,{field:'guid', title: 'ID',hide:true}
            ]
            ]
            ,toolbar: '#toolbar'
            ,height:'full-' + ( $('.north').height()+296)
            ,width:'full-' + 260
        });
        //监听表格排序问题
        table.on('sort(ftable)', function (obj) {
            $.tablelist.sort('ftable',obj);
        });
        //头工具栏事件
        table.on('toolbar(ftable1)', function(obj){
            if(obj.event == 'addorgmanager')
                addUser('/system/organization/addorgmanagerspost','新增管理员','managers');
        });
        table.on('toolbar(ftable)', function(obj){
            if(obj.event == 'adduser')
                addUser('/system/organization/adduserpost','新增员工','users');
        });
        //监听工具条
        table.on('tool(ftable1)', function (obj) {
            var data = obj.data;
            if(obj.event === 'del'){ //删除
                var pdata = {'guid':pguid,'orgid':data.userId}
                $.modal.confirmCom('确定要删除么？',function(){deleteUser('/system/organization/deleteorgmanagerspost',pdata)});
            }
        });
        table.on('tool(ftable)', function (obj) {
            var data = obj.data;
            if(obj.event === 'del'){ //删除
                var pdata = {'orgid':data.userId}
                $.modal.confirmCom('确定要删除么？',function(){deleteUser('/system/organization/deleteuserpost',pdata)});
            }
        });

        $('#btn-search').on('click', function () {
            var userName = $("#vname").val();
            dtree.reload(CommonTreet,{
                elem: "#dhtree",
                data: getTree('/system/organization/listtreejson'),
            });
            table.reload('dhtable1', {url: "/system/organization/managersAlllistjson?orgPathAll=orgPathAll",page:{curr: 1}});
            table.reload('dhtable', {url: "/system/user/list?moduleTable=TS_userinfo&userName="+userName,page:{curr: 1}});
        });
        //删除操作
        function deletepost(orgPathAll){
            var deldata = {'orgPathAll':orgPathAll};
            $.operate.post('/system/organization/deletesubtreepost',deldata,function(result){
                allReload(result)
            });
        }
        /*
        *url:保存路径
        *title:弹出层标题
        *type:managers:新增管理员；users:新增员工
        */
        function addUser(url,title,type){
            if(pguid){
            	if(type == 'users' && vtype == 'company'){
            		layer.msg("用户不能直接分配在公司节点，请选择部门节点进行分配。");
            		return false;
            	}
                $.modal.open(title,'/system/organization/orgusersadd?pagetype='+type+'&guid='+pguid+'&orgKind='+vtype,'700','500',function(index){
                    var res = window["layui-layer-iframe" + index].callbackdata();
                    if(res.length > 0){
                    	var userList = [];
                        if(type == 'managers'){
                        	$.each(res,function(index,element){
                        		userList.push({'orgid':element['userId']})
                        	})
                            var pdata = {'guid':pguid,'userList':JSON.stringify(userList)};
                        }else{
                        	$.each(res,function(index,element){
                        		userList.push({'orgid':element['userId'],'orgName':element['userName']})
                        	})
                            var pdata = {'pguid':pguid,'userList':JSON.stringify(userList)}
                        }

                        $.operate.post(url,pdata,function(result){
                            allReload(result)
                        });
                    }else{
                        layer.msg("请选择人员！");
                    }
                })
            }else{
//     			$.modal.alert("请选择节点",'warning');
                layer.msg("请选择节点");
            }
        }
        /*
        *删除管理员、员工
        *url:删除路径
        *padata:条件对象
        */
        function deleteUser(url,pdata){
            $.operate.post(url,pdata,function(result){
                allReload(result)
            });
        }
        //重新加载
        function allReload(result){
            if(result.code==0){
                setTimeout(function(){
                    layer.closeAll();
                    table.reload('dhtable1', {url: "/system/organization/managersAlllistjson?orgPathAll="+orgPathAll,page:{curr: 1}});
                    table.reload('dhtable', {url: "/system/organization/userlistjson?orgPathAll="+orgPathAll,page:{curr: 1}});
                    dtree.reload(CommonTreet,{
                        elem: "#dhtree",
                        data: getTree('/system/organization/listtreejson'),
                        done: function(){
                            var params = dtree.dataInit("dhtree", pguid);
                        }
                    });
                }, 1000);
            }
        }
    });
</script>
</body>
</html>